package com.junzhiit.nengji.shop.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.junzhiit.nengji.bo.UserEvaluateBO;
import com.junzhiit.nengji.dao.GoodsDAO;
import com.junzhiit.nengji.dao.OrdersDAO;
import com.junzhiit.nengji.dao.OrdersGoodsDAO;
import com.junzhiit.nengji.dao.ShopDAO;
import com.junzhiit.nengji.dao.UserDAO;
import com.junzhiit.nengji.dao.UserEvaluateDAO;
import com.junzhiit.nengji.model.Goods;
import com.junzhiit.nengji.model.Orders;
import com.junzhiit.nengji.model.OrdersGoods;
import com.junzhiit.nengji.model.Shop;
import com.junzhiit.nengji.model.User;
import com.junzhiit.nengji.model.UserEvaluate;
import com.junzhiit.nengji.model.enums.Is;
import com.junzhiit.nengji.model.enums.OrderGoodsStatus;
import com.junzhiit.nengji.model.enums.OrderStatus;
import com.junzhiit.nengji.shop.service.UserEvaluateService;
import com.junzhiit.nengji.shop.web.exception.GoodsNotFoundException;
import com.junzhiit.nengji.shop.web.exception.OrdersGoodsNotFoundException;
import com.junzhiit.nengji.shop.web.exception.ShopNotFoundException;
import com.junzhiit.nengji.shop.web.exception.UserNotFoundException;
import com.junzhiit.nengji.shop.web.util.aliyun.AliyunOSSClientUtil;
import com.junzhiit.nengji.shop.web.vo.ResultVO;
import com.junzhiit.nengji.shop.web.vo.UserEvaluateAddVO;
import com.junzhiit.nengji.shop.web.vo.UserEvaluateVO;

@Service
public class UserEvaluateServiceImpl extends BaseServiceImpl implements UserEvaluateService {

	@Autowired
	private UserEvaluateDAO userEvaluateDAO;
	
	@Autowired
	private ShopDAO shopDAO;
	
	@Autowired
	private OrdersGoodsDAO ordersGoodsDAO;
	
	@Autowired
	private UserDAO userDAO;
		
	@Autowired
	private GoodsDAO goodsDAO;
	
	@Autowired
	private OrdersDAO ordersDAO;
	
	@Value("${upload.temp}")
	private String uploadPath;

	@Value("${upload.mall.evaluate}")
	private String userEvaluatePath;
	
	//阿里云API的bucket名称
	@Value("${oss.backet.name}")
	private String backetName;
	
	@Override
	public ResultVO findUserEvaluate(Integer pageNumber,Integer maxPageRow,String goodsId,Integer status, String evaluateTagId) {
		ResultVO result =new ResultVO();
		Map<String, Object> map=new HashMap<String, Object>();
		
		Goods goods=goodsDAO.query(goodsId);
		if(goods==null){
			throw new GoodsNotFoundException("商品没找到");
		}
			
		List<UserEvaluateBO> userEvaluateBOList= userEvaluateDAO.findUserEvaluate(pageNumber,maxPageRow,goodsId,status, evaluateTagId);
		List<UserEvaluateVO> userEvaluateVOList =new ArrayList<UserEvaluateVO>();
		for (UserEvaluateBO userEvaluateBO : userEvaluateBOList) {
			UserEvaluateVO userEvaluateVO=new UserEvaluateVO();
			BeanUtils.copyProperties(userEvaluateBO, userEvaluateVO);
			
			String Images = userEvaluateBO.getImage();
			List<String> imagesList=new ArrayList<String>();
			if (StringUtils.isNotBlank(Images)) {
				imagesList = Arrays.asList(Images.split(","));
			}
			userEvaluateVO.setImages(imagesList);
			
			if(userEvaluateVO.getIsAnonymity()==Is.YES.toIntValue()){
				userEvaluateVO.setNickName("匿名用户");
				userEvaluateVO.setHeadImgUrl("https://file.dev.legeclub.com/evaluate/anonymous_head.jpg");
			}
			
			userEvaluateVOList.add(userEvaluateVO);
		}
		map.put("userEvaluateList", userEvaluateVOList);
		
		result.setData(map);
		return result;
	}

	@Override
	@Transactional
	public ResultVO add(UserEvaluateAddVO userEvaluateAddVO) {
		ResultVO result=new ResultVO();
		
		Shop shop=shopDAO.query(userEvaluateAddVO.getShopId());
		if (shop == null) {
			throw new ShopNotFoundException("店铺不存在");
		}
		User user=userDAO.query(userEvaluateAddVO.getUserId());
		if (user == null) {
			throw new UserNotFoundException("用户不存在");
		}
		Goods goods=goodsDAO.query(userEvaluateAddVO.getGoodsId());
		if (goods == null) {
			throw new GoodsNotFoundException("商品不存在");
		}
		OrdersGoods ordersGoods=ordersGoodsDAO.query(userEvaluateAddVO.getOrdersGoodsId());
		if (ordersGoods == null) {
			throw new OrdersGoodsNotFoundException("订单商品不存在");
		}
		
		ordersGoods.setStatus(OrderGoodsStatus.ESTIMATE.toIntValue());
		ordersGoods.setUpdatedTime(new Date());
		ordersGoodsDAO.update(ordersGoods);
		
		int count =ordersGoodsDAO.countNotEvaluateOrderGoods(ordersGoods.getOrderId());
		if(count==0){
			Orders orders=new Orders();
			orders.setId(ordersGoods.getOrderId());
			orders.setStatus(OrderStatus.COMPLETE.toIntValue());
			orders.setUpdatedTime(new Date());
			ordersDAO.update(orders);
		}
		
		UserEvaluate userEvaluate=new UserEvaluate();
		BeanUtils.copyProperties(userEvaluateAddVO, userEvaluate);
		String[] images=userEvaluateAddVO.getImages();
		if(images!=null){
			String image=String.join(",", images);
			userEvaluate.setImage(image);
			for (String string : images) {
				//FileUtil.moveFile(uploadPath + string, userEvaluatePath);
				boolean is = AliyunOSSClientUtil.moveFile(backetName, string, userEvaluatePath, uploadPath);
				if(is){
					AliyunOSSClientUtil.removeFile(backetName, uploadPath+string);
				}
			}
		}
		result.setData(userEvaluateDAO.insert(userEvaluate));
		
		return result;
	}

}
